package com.cleaningcloud.cld.service.impl;

import com.cleaningcloud.cld.domain.CldCalendar;
import com.cleaningcloud.cld.domain.model.CalendarConfigDTO;
import com.cleaningcloud.cld.domain.model.MxnzpCalendarDTO;
import com.cleaningcloud.cld.domain.model.MxnzpResponseData;
import com.cleaningcloud.cld.mapper.CldCalendarMapper;
import com.cleaningcloud.cld.service.ICldCalendarService;
import com.cleaningcloud.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;

/**
 * 日历Service业务层处理
 *
 * @author cleaningcloud
 * @date 2024-11-08
 */
@Service
public class CldCalendarServiceImpl implements ICldCalendarService {
    private static final String URL = "https://www.mxnzp.com/api/holiday/list/year/%d?ignoreHoliday=false&app_id=tldkn1hkkoofques&app_secret=kV9qgeRJ5baZworKApsWVbHS3hNxndYZ";
    @Autowired
    private CldCalendarMapper cldCalendarMapper;
    @Autowired
    private RestTemplate restTemplate;

    /**
     * 查询日历
     *
     * @param calendarId 日历主键
     * @return 日历
     */
    @Override
    public CldCalendar selectCldCalendarByCalendarId(Long calendarId) {
        return cldCalendarMapper.selectCldCalendarByCalendarId(calendarId);
    }

    /**
     * 查询日历列表
     *
     * @param cldCalendar 日历
     * @return 日历
     */
    @Override
    public List<CldCalendar> selectCldCalendarList(CldCalendar cldCalendar) {
        return cldCalendarMapper.selectCldCalendarList(cldCalendar);
    }

    /**
     * 新增日历
     *
     * @param cldCalendar 日历
     * @return 结果
     */
    @Override
    public int insertCldCalendar(CldCalendar cldCalendar) {
        cldCalendar.setCreateTime(DateUtils.getNowDate());
        return cldCalendarMapper.insertCldCalendar(cldCalendar);
    }

    /**
     * 修改日历
     *
     * @param cldCalendar 日历
     * @return 结果
     */
    @Override
    public int updateCldCalendar(CldCalendar cldCalendar) {
        return cldCalendarMapper.updateCldCalendar(cldCalendar);
    }

    /**
     * 批量删除日历
     *
     * @param calendarIds 需要删除的日历主键
     * @return 结果
     */
    @Override
    public int deleteCldCalendarByCalendarIds(Long[] calendarIds) {
        return cldCalendarMapper.deleteCldCalendarByCalendarIds(calendarIds);
    }

    /**
     * 删除日历信息
     *
     * @param calendarId 日历主键
     * @return 结果
     */
    @Override
    public int deleteCldCalendarByCalendarId(Long calendarId) {
        return cldCalendarMapper.deleteCldCalendarByCalendarId(calendarId);
    }

    @Override
    public void generateCalendarTask() {
        LocalDate today = DateUtils.getNextYearToday();
        int year = today.getYear();
        // 查询是否已生成过
        CldCalendar query = new CldCalendar();
        query.setCalendarYear(year);
        List<CldCalendar> calendarList = cldCalendarMapper.selectCldCalendarList(query);
        if (calendarList.size() > 0) {
            return;
        }
        ResponseEntity<MxnzpResponseData> responseEntity = restTemplate.getForEntity(String.format(URL, year), MxnzpResponseData.class);
        HttpStatus statusCode = responseEntity.getStatusCode();
        if (!HttpStatus.OK.equals(statusCode)) {
            return;
        }

        MxnzpResponseData responseData = responseEntity.getBody();
        if (responseData != null && !responseData.getCode().equals(1)) {
            return;
        }

        List<MxnzpCalendarDTO> data = responseData.getData();
        if (CollectionUtils.isEmpty(data)) {
            return;
        }
        List<CldCalendar> newConfigList = new ArrayList<>();
        for (MxnzpCalendarDTO monthData : data) {
            List<CalendarConfigDTO> days = monthData.getDays();
            for (CalendarConfigDTO day : days) {
                CldCalendar calendarConfig = new CldCalendar();
                calendarConfig.setCalendarYear(monthData.getYear());
                calendarConfig.setCalendayMonth(monthData.getMonth());
                // 提前日期
                LocalDate localDay = DateUtils.parseDate(day.getDate()).toInstant()
                        .atZone(ZoneId.systemDefault())
                        .toLocalDate();
                calendarConfig.setCalendayDay(localDay.getDayOfMonth());
                calendarConfig.setCalendayDate(day.getDate());
                calendarConfig.setCalendayType(day.getType());
                calendarConfig.setCalendayTypeDes(day.getTypeDes());
                calendarConfig.setWeekDay(day.getWeekDay());
                calendarConfig.setDayOfYear(day.getDayOfYear());
                calendarConfig.setWeekOfYear(day.getWeekOfYear());
                calendarConfig.setIndexWorkDayMonth(day.getIndexWorkDayOfMonth());
                calendarConfig.setLunarCalendar(day.getLunarCalendar());
                newConfigList.add(calendarConfig);
            }
        }

        if (!CollectionUtils.isEmpty(newConfigList)) {
            cldCalendarMapper.batchCldCalendarInsert(newConfigList);
        }

    }

}
